package aos.ydksxt.service.common;

import aos.framework.core.service.AOSBaseService;
import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjRyxx;
import aos.ydksxt.dao.DjRyxxKz;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 信息采集管理公共服务
 * Created by Stiofan on 2016/11/24.
 */
@Service
public abstract class CommonService extends AOSBaseService {

    protected Logger logger = LogManager.getLogger();

    public abstract void initView(HttpModel httpModel);

    @Transactional
    public void queryXzqhszDmBySjxzqhszDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("sjxzqhszDm", inDto.get("sjxzqhszDm"));
        String returnJson = null;
        if (!ObjectUtils.anyNotNull(redisCacheService.get("xzqhszDmBySjxzqhszDm-" + param.getString("sjxzqhszDm")))) {
            Object object = null;
            if (StringUtils.isEmpty(param.getString("sjxzqhszDm"))) {
                object = persistenceDAO.findAllBySql("select xzqhsz_dm as value, xzqhmc as display\n" +
                        "  from hx_dm_qg.dm_gy_xzqh qh\n" +
                        " where qh.xzqhsz_dm = '620500'");
            } else {
                object = persistenceDAO.findAllBySql("select xzqhsz_dm as value, xzqhmc as display\n" +
                        "  from hx_dm_qg.dm_gy_xzqh qh\n" +
                        " where qh.sjxzqhsz_dm =:sjxzqhszDm", param);
            }
            returnJson = AOSJson.toJson(AOSUtils.object2Map(object));
            redisCacheService.set("xzqhszDmBySjxzqhszDm-" + param.getString("sjxzqhszDm"), returnJson, 60);
        } else {
            returnJson = redisCacheService.get("xzqhszDmBySjxzqhszDm-" + param.getString("sjxzqhszDm"));
        }
        httpModel.setOutMsg(returnJson);
    }

    @Transactional
    public void queryJdxzDmByXzqhszDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("xzqhszDm", inDto.get("xzqhszDm"));
        String returnJson = null;
        if (!ObjectUtils.anyNotNull(redisCacheService.get("jdxzDmByXzqhszDm-" + param.getString("xzqhszDm")))) {
            Object object = persistenceDAO.findAllBySql("select jdxz_dm as value, jdxzmc as display\n" +
                    "  from hx_dm_zdy.dm_gy_jdxz\n" +
                    " where xzqhsz_dm = :xzqhszDm\n" +
                    "   and xybz = 'Y'\n" +
                    "   and yxbz = 'Y'", param);
            returnJson = AOSJson.toJson(AOSUtils.object2Map(object));
            redisCacheService.set("jdxzDmByXzqhszDm-" + param.getString("xzqhszDm"), returnJson, 60);
        } else {
            returnJson = redisCacheService.get("jdxzDmByXzqhszDm-" + param.getString("xzqhszDm"));
        }
        httpModel.setOutMsg(returnJson);
    }

    @Transactional
    public void querySqjqDmByJdxzDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("jdxzDm", inDto.get("jdxzDm"));
        String returnJson = null;
        if (StringUtils.isEmpty(redisCacheService.get("sqjqDmByJdxzDm-" + param.getString("jdxzDm")))) {
            Object object = persistenceDAO.findAllBySql("select t.sqjq_dm as value, jq.sqjqmc as display\n" +
                    "  from cs_dj_sqjq t, dm_dj_sqjq jq\n" +
                    " where t.sqjq_dm = jq.sqjq_dm\n" +
                    "   and t.jdxz_dm = :jdxzDm\n" +
                    "   and t.xybz = 'Y'\n" +
                    "   and t.yxbz = 'Y'", param);
            returnJson = AOSJson.toJson(AOSUtils.object2Map(object));
            redisCacheService.set("sqjqDmByJdxzDm-" + param.getString("jdxzDm"), returnJson, 60);
        } else {
            returnJson = redisCacheService.get("sqjqDmByJdxzDm-" + param.getString("jdxzDm"));
        }
        httpModel.setOutMsg(returnJson);
    }

    @Transactional
    public void queryDlxdDmBySqjqDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("sqjqDm", inDto.get("sqjqDm"));
        String returnJson = null;
        if (StringUtils.isEmpty(redisCacheService.get("dlxdDmBySqjqDm-" + param.getString("xzqhszDm")))) {
            Object object = persistenceDAO.findAllBySql("select t.dlxd_dm as value, xd.dlxdmc as display\n" +
                    "  from cs_dj_dlxd t, dm_dj_dlxd xd\n" +
                    " where t.dlxd_dm = xd.dlxd_dm\n" +
                    "   and t.sqjq_dm = :sqjqDm\n" +
                    "   and t.xybz = 'Y'\n" +
                    "   and t.yxbz = 'Y'", param);
            returnJson = AOSJson.toJson(AOSUtils.object2Map(object));
            redisCacheService.set("dlxdDmBySqjqDm-" + param.getString("sqjqDm"), returnJson, 60);
        } else {
            returnJson = redisCacheService.get("dlxdDmBySqjqDm-" + param.getString("sqjqDm"));
        }
        httpModel.setOutMsg(returnJson);
    }

    @Transactional
    public String getFullAddress(String xzqhdsDm, String xzqhxqDm, String jdxzDm, String sqjqDm, String dlxdDm, String dzlh) {
        String queryString = "select (select ds.xzqhmc\n" +
                "          from hx_dm_qg.dm_gy_xzqh ds\n" +
                "         where ds.xzqhsz_dm = :xzqhdsDm) ||\n" +
                "       (select ds.xzqhmc\n" +
                "          from hx_dm_qg.dm_gy_xzqh ds\n" +
                "         where ds.xzqhsz_dm = :xzqhxqDm) ||\n" +
                "       (select xz.jdxzmc\n" +
                "          from hx_dm_zdy.dm_gy_jdxz xz\n" +
                "         where xz.jdxz_dm = :jdxzDm) ||\n" +
                "       (select jq.sqjqmc from dm_dj_sqjq jq where jq.sqjq_dm = :sqjqDm) ||\n" +
                "       (select xd.dlxdmc from dm_dj_dlxd xd where xd.dlxd_dm = :dlxdDm) dzmc\n" +
                "  from dual";
        Dto param = Dtos.newDto();
        param.put("xzqhdsDm", xzqhdsDm);
        param.put("xzqhxqDm", xzqhxqDm);
        param.put("jdxzDm", jdxzDm);
        param.put("sqjqDm", sqjqDm);
        param.put("dlxdDm", dlxdDm);
        try {
            List results = persistenceDAO.findAllBySql(queryString, param);
            if (results.size() != 0) {
                return ((Map) results.get(0)).get("dzmc") + (dzlh == null ? "" : dzlh);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }

    }
    @Transactional
    public boolean checkSsgly(String ssglyDm) {
        Dto param = Dtos.newDto("account", ssglyDm);
        List results = persistenceDAO.findAllBySql("select r.NAME||'('||r.ACCOUNT||')' as ssgly from AOS_USER r where r.ACCOUNT=:account and r.is_del='0'", param);
        if (results.size() == 0) {
            return false;
            //outDto.setAppCode("err");
            //outDto.setAppMsg("当前税收管理员代码已经无效或错误，请重新调整税收管理员！");
        } else if (results.size() > 0) {
            return false;
            //Map ssglyMap = (Map) results.get(0);
            //outDto.setAppCode("war");
            //outDto.setAppMsg("对不起，您无权对此进行操作，请联系" + ssglyMap.get("ssgly"));
        } else {
            return true;
            //outDto.setAppCode("suc");
        }
    }
    @Transactional
    public void checkSsgly(Dto outDto, String ssglyDm) {
        Dto param = Dtos.newDto("account", ssglyDm);
        List results = persistenceDAO.findAllBySql("select r.NAME||'('||r.ACCOUNT||')' as ssgly from AOS_USER r where r.ACCOUNT=:account and r.is_del='0'", param);
        if (results.size() == 0) {
            outDto.setAppCode("err");
            outDto.setAppMsg("当前税收管理员代码已经无效或错误，请重新调整税收管理员！");
        } else if (results.size() > 0) {
            Map ssglyMap = (Map) results.get(0);
            outDto.setAppCode("war");
            outDto.setAppMsg("对不起，您无权对此进行操作，请联系" + ssglyMap.get("ssgly"));
        } else {
            outDto.setAppCode("suc");
        }
    }

    @Transactional
    public void queryZgswjgDmByHttpModel(HttpModel httpModel) {
        Dto param = Dtos.newDto("swjgDm", httpModel.getUserModel().getOrg_id());
        List results = persistenceDAO.findAllBySql("select a.swjg_dm as value, a.swjgjc as display\n" +
                "  from hx_dm_zdy.dm_gy_swjg a\n" +
                " where a.swjg_dm in (select b.sjswjg_dm\n" +
                "                       from hx_dm_zdy.dm_gy_swjg b\n" +
                "                      where b.yxbz = 'Y'\n" +
                "                        and b.xybz = 'Y'\n" +
                "                      start with b.swjg_dm = :swjgDm\n" +
                "                     connect by prior b.swjg_dm = b.sjswjg_dm)", param);
        httpModel.setOutMsg(AOSJson.toJson(results));
    }
    @Transactional
    public void queryZgswskfjDmByZgswjgDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("zgswjgDm", inDto.get("zgswjgDm"));
        List results = persistenceDAO.findAllBySql("select jg.swjg_dm as value, jg.swjgjc as display\n" +
                "  from hx_dm_zdy.Dm_Gy_Swjg jg\n" +
                " where jg.sjswjg_dm = :zgswjgDm\n" +
                "   and jg.yxbz = 'Y'\n" +
                "   and jg.xybz = 'Y'" , param);
        httpModel.setOutMsg(AOSJson.toJson(results));
    }
    @Transactional
    public void querySsglyDmByZgswskfjDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("zgswskfjDm", inDto.get("zgswskfjDm"));
        List results = persistenceDAO.findAllBySql("   select c.swry_dm as value, c.swryxm as display\n" +
                "     from hx_dm_zdy.dm_gy_swry c\n" +
                "    where c.swry_dm in\n" +
                "          (select ry.swry_dm\n" +
                "             from hx_qx.qx_jggw_swrysf sf, hx_qx.Dm_Qx_Swrysf ry\n" +
                "            where sf.swrysf_dm = ry.swrysf_dm\n" +
                "              and sf.yxbz = 'Y'\n" +
                "              and ry.sfswjg_dm = :zgswskfjDm)\n", param);
        httpModel.setOutMsg(AOSJson.toJson(results));
    }

    public void queryRyjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto queryParam = Dtos.newDto();
        queryParam.put("nsrsbh", inDto.get("nsrsbh"));
        queryParam.put("shxydm", inDto.get("shxydm"));
        queryParam.put("zzjgdm", inDto.get("zzjgdm"));
        queryParam.put("djxh", inDto.getBigDecimal("djxh"));
        DjRyxx djRyxx = new DjRyxx();
        DjRyxxKz djRyxxKz = new DjRyxxKz();
        List ydksResults = persistenceDAO.findAllByHql("from DjRyxx a,DjRyxxKz b where a.djxh=b.djxh and (a.nsrsbh=:nsrsbh or b.shxydm=:shxydm or b.zzjgdm=:zzjgdm or a.djxh=:djxh) and a.yxbz='Y'", queryParam);
        List newYdksResults = new ArrayList();
        Dto outDto = Dtos.newOutDto();
        for (Object ydksResult : ydksResults) {
            Object[] objects = (Object[]) ydksResult;
            for (Object object : objects) {
                if (object instanceof DjRyxx) {
                    djRyxx = (DjRyxx) object;
                } else if (object instanceof DjRyxxKz) {
                    djRyxxKz = (DjRyxxKz) object;
                }
            }
            if (inDto.getBigDecimal("djxh") != null && inDto.getBigDecimal("djxh").equals(djRyxx.getDjxh())) {
                outDto.putAll((Map) AOSUtils.object2Map(djRyxx));
                outDto.putAll((Map) AOSUtils.object2Map(djRyxxKz));
                if (!httpModel.getUserModel().getAccount().equals(djRyxx.getSsglyDm()) && djRyxx.getSsglyDm() != null) {//判断税收管理员代码与当前操作人员不一致，给我提示
                    checkSsgly(outDto, djRyxx.getSsglyDm());
                }
                outDto.put("lxrdz", getFullAddress(djRyxx.getLxrdzXzqhdsDm(), djRyxx.getLxrdzXzqhxqDm(), djRyxx.getLxrdzJdxzDm(), djRyxx.getLxrdzSqjqDm(), djRyxx.getLxrdzDlxdDm(), djRyxx.getLxrdzLh()));
                httpModel.setOutMsg(AOSJson.toJson(outDto));
                return;
            }
            Map tmpMap = new HashMap();
            tmpMap.putAll((Map) AOSUtils.object2Map(djRyxx));
            tmpMap.putAll((Map) AOSUtils.object2Map(djRyxxKz));
            tmpMap.put("lxrdz", getFullAddress(djRyxx.getLxrdzXzqhdsDm(), djRyxx.getLxrdzXzqhxqDm(), djRyxx.getLxrdzJdxzDm(), djRyxx.getLxrdzSqjqDm(), djRyxx.getLxrdzDlxdDm(), djRyxx.getLxrdzLh()));
            newYdksResults.add(tmpMap);
        }
        if (newYdksResults.size() == 0) {
            outDto.setAppCode("noRyxx");
            httpModel.setOutMsg(AOSJson.toJson(outDto));
        } else {
            outDto.setAppCode("suc");
            httpModel.setOutMsg(AOSJson.toJson(AOSUtils.object2Map(newYdksResults.get(0))));
        }

    }

    protected String getCzryDm(HttpModel httpModel) {
        return httpModel.getUserModel().getAccount();
    }

    protected String getCzjgDm(HttpModel httpModel) {
        return String.valueOf(httpModel.getUserModel().getOrg_id());
    }

    protected Timestamp getDateTime() {
        return AOSUtils.getDateTime();
    }

    protected String getRandomUUID() {
        return AOSUtils.getRandomUUID();
    }

    protected String getSybh(String xzqhxqDm) {
        return xzqhxqDm + AOSUtils.getDateStr("yyyyMMddHHmmss");
    }

}
